package cn.hh.file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 *
 * 根据 原代码拷贝 编译后文件
 * @Author huangh
 * @date 2021/07/21
 * @Description:
 */
public class FileCopy {

    //code.txt
    //D:\work\java\zzzd-cloud\atm_api
    //C:\Users\hh\Desktop\打包\自助终端云化\atm_api

    //需要拷贝代码路径
    String codeAddr = "/file/code.txt";
    //项目根路径
    String ProjectSrc = "D:\\work\\java\\zzzd-cloud\\atm_api";
    //打包路径
    String pakFile = "C:\\Users\\hh\\Desktop\\打包\\自助终端云化\\20211210自助终端电子历史账单无打印按钮\\eatm_app";
    //编译路径
    String compileFile = "target\\classes";

    public void copy(){
        String basePath2 = System.getProperty("user.dir");
        File file = new File(basePath2 + codeAddr);

        List<String> list = FileUtil.readLines(file,"UTF-8");
        for (int i=0;i<list.size();i++){
            String cont= list.get(i);
            System.out.println(cont);
            try{
                boolean copyFlag = false;
                //复制操作
                //去除项目  开头src\main\java,src\main\resources,pom 文件自己复制去 ,替换成target\classes
                String copyFileAddr = "";
                //处理 src\main\java
                if(cont.indexOf("src\\main\\java") !=-1){
                    copyFileAddr = cont.replace("src\\main\\java","target\\classes");
                    copyFlag = true;
                }
                //处理 src\main\resources
                if(cont.indexOf("src\\main\\resources") !=-1){
                    copyFileAddr = cont.replace("src\\main\\resources","target\\classes");
                    copyFlag = true;
                }
                if(copyFlag){
                    //如果是java文件编译出来是class
                    if(copyFileAddr.endsWith(".java")){
                        copyFileAddr = copyFileAddr.replace(".java",".class");
                    }
                    String TagetFileAddr = copyFileAddr.replace(ProjectSrc,pakFile);
                    FileUtil.copy(copyFileAddr,TagetFileAddr,true);

                    //内部类 匿名类处理 === start ================
                    //判断还有没有内部类 匿名类 https://blog.csdn.net/qq_40028198/article/details/105345822
                    String copyDir = "";
                    String mainClass = "";
                    //获取最后一个\ 位置
                    int copyFileAddrLastIndex  = copyFileAddr.lastIndexOf("\\");
                    copyDir = copyFileAddr.substring(0,copyFileAddrLastIndex);
                    //+1 去掉/
                    mainClass = copyFileAddr.substring(copyFileAddrLastIndex+1,copyFileAddr.length());
                    String[] mainClassArr = mainClass.split("\\.");
                    Path path = Paths.get(copyDir);
                    //  A\$.+\.class
                    Pattern pattern = Pattern.compile(mainClassArr[0]+"\\$.+\\.class");
                    List<Path> paths = Files.walk(path).filter(p -> {
                        //如果不是普通的文件，则过滤掉
                        if(!Files.isRegularFile(p)) {
                            return false;
                        }
                        File file2 = p.toFile();
                        Matcher matcher = pattern.matcher(file2.getName());
                        return matcher.matches();
                    }).collect(Collectors.toList());

                    for(Path item : paths) {
                        //System.out.println(item.toFile().getPath());
                        String copyFileAddrSon = item.toFile().getPath();
                        String TagetFileAddrSon = copyFileAddrSon.replace(ProjectSrc,pakFile);
                        FileUtil.copy(copyFileAddrSon,TagetFileAddrSon,true);
                    }
                    //内部类 匿名类处理 === end ================
                }

            }catch (Exception e){
                System.out.println("错误文件 "+cont);
                e.printStackTrace();
            }


        }

    }




}
